package com.web.controller;

import com.alibaba.fastjson2.JSON;
import com.web.entity.PageVO;
import com.web.entity.ResponseResult;
import com.web.service.StudentService;
import com.web.service.impl.StudentServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/findStudentPage")
public class FindStudentsPageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求编码
        req.setCharacterEncoding("UTF-8");
        //设置响应编码
        resp.setCharacterEncoding("UTF-8");
        //设置响应类型
        resp.setContentType("application/json;charset=UTF-8");
        //获得参数
        String current=req.getParameter("current");
        String pageSize=req.getParameter("pageSize");
        int cur=1;
        if(current!=null){
            cur=Integer.parseInt(current);
        }
        int pSize=10;
        if(pageSize!=null){
            pSize=Integer.parseInt(pageSize);
        }
        StudentService studentService=new StudentServiceimpl();
        PageVO students=studentService.findStudentPage(cur,pSize);
        //封装到响应结果中
        ResponseResult result=new ResponseResult(200, students, "ok");
        //发送对象给前端
        resp.getWriter().write(JSON.toJSONString(result));
    }
}
